package com.springmybatis;

import com.lpy.mapper.UserMapper;
import com.lpy.service.UserService;
import com.sun.media.sound.SoftTuning;
import net.sf.cglib.proxy.Enhancer;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Description:
 * @author: LiuPengYu
 * @date 2024/11/21 16:37
 */
//@Component
public class LpyFactoryBean implements FactoryBean {

    private SqlSession session;

    private Class mapperClass;

    public LpyFactoryBean(Class mapperClass) {
        this.mapperClass = mapperClass;
    }

    @Autowired
    public void setSession(SqlSessionFactory sessionFactory) {
        sessionFactory.getConfiguration().addMapper(mapperClass);
        this.session = sessionFactory.openSession();
    }

    @Override
    public Object getObject() throws Exception {
        Object mapper = session.getMapper(mapperClass);
         return mapper;
    }

    @Override
    public Class<?> getObjectType() {
        return mapperClass;
    }
}
